package csu.jpetstore.jpetstore.controller;

import csu.jpetstore.jpetstore.domain.Product;
import csu.jpetstore.jpetstore.repository.ProductRepository;
import csu.jpetstore.jpetstore.result.Result;
import csu.jpetstore.jpetstore.result.ResultFactory;
import csu.jpetstore.jpetstore.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping(value = "/api")
public class ProductController {
    @Autowired
    ProductService productService;

    @GetMapping("/category")
    public Result getCategory(){
        return null;
    }

    @GetMapping("/category/{categoryId}")
    public Result getProducts(@PathVariable String categoryId){
        return null;
    }

    @GetMapping("/category/{categoryId}/product/{productId}")
    public Result getItems(@PathVariable String categoryId,@PathVariable String productId){
        return null;
    }

    @GetMapping("/item/{itemId}")
    public Result getItemById(@PathVariable String itemId){
        return null;
    }

    @PostMapping("/search")
    public Result search(@RequestBody String keyword){
        keyword = HtmlUtils.htmlEscape(keyword);
        List<Product> products = productService.getAllByNameLike(keyword);
        if (products.size() == 0)
            return ResultFactory.getFail("未搜索到",null);
        return ResultFactory.getSuccess("搜索成功",products);
    }
}
